import axios from '../plugins/axiosInstance'
import { useStore, pinia } from '../stores/index';

const store = useStore(pinia);
/**
 * 获取登录用户的歌单
 */
export const getMyMusicList = async () => {
    axios.get('/musicList/getUserMusicList').then((res: any) => {
        if (res.data.code == 1) {
            console.log("获取歌单", res.data.data);
            store.setMyMusicList(res.data.data);
        }
    })
}
export const getUserCreateMusicList = async (userId?: string) => {
    return axios.get('/musicList/getUserMusicList', { params: { userId } })
}
export const getComments = async (typeId: string, type: string, currentPage: number, pageSize: number, sortByLikes: boolean = false) => {
    return axios({
        url: '/comment/getComment',
        method: 'get',
        params: {
            currentPage,
            pageSize,
            type,
            typeId,
            sortByLikes
        },
    })
}
export const getCommentCount = async (postId: string, type: string) => {
    return axios({
        url: '/post/getCommentCount',
        method: 'get',
        params: {
            postId,
            type
        },
    })
}

export const commentCreate = (content: string, type: string, typeId: string) => {
    return axios.post('/comment/sendComment', {
        content: content,
        type,
        typeId
    })
}

export const postCreate = (content: string, imageUrls?: string[]) => {
    return axios.post('/post/create', {
        content: content,
        imageList: imageUrls
    })
}
/**
 * 检查注销后是否需要返回主页
 * @param route route-name
 * @returns boolean 
 */
export const checkLogoutNeedReturnToMainPage = (route: any) => {
    const routeList = ['personCenter', 'myMusic', 'myMusicList', 'mySinger', 'listEdit', 'Friends']
    for (let r of routeList) {
        if (route.name === r) {
            return true
        }
    }
    return false
}
/**
 * 获取音乐信息
 * @param musicId 音乐id
 * @returns Promise
 */
export const getMusic = (musicId: string) => {
    return axios({
        url: 'music/getMusic',
        params: {
            musicId
        }
    })
}

export const getOnePost = (postId: string) => {
    return axios({
        url: '/post/getOne',
        params: {
            postId,
        }
    })
}

/**
 * 获取动态列表
 * @param currentPage 当前页
 * @param pageSize 页面尺寸
 * @returns Promise
 */
export const getPostList = async (currentPage: number, pageSize: number) => {
    return axios({
        url: '/post/getPost',
        method: 'get',
        params: {
            currentPage: currentPage,
            pageSize: pageSize
        }
    })
}

/**
 * 获取用户自己的动态
 * @param currentPage 当前页
 * @param pageSize 页面尺寸
 * @returns Promise
 */
export const getMyPostList = async (currentPage: number, pageSize: number, userId?: string) => {
    return axios({
        url: '/post/getMyPost',
        method: 'get',
        params: {
            currentPage,
            pageSize,
            userId
        }
    })
}

/**
 * 获取用户的公开信息
 * @param userId 用户ID
 * @returns  Promise
 */
export const getPublicUserInfo = async (userId: string) => {
    return axios({
        url: '/user/getPublicInfo',
        method: 'GET',
        params: {
            userId
        }
    })
}
/**
 * 获取歌手的50首歌
 * @param userId 用户ID
 * @returns Promise
 */
export const getSinger50Music = async (userId: string) => {
    return axios({
        url: '/music/singer',
        method: 'GET',
        params: {
            userId
        }
    })
}
/**
 * 获取某个用户的社交信息 动态/粉丝/关注数
 * @param id 用户ID
 * @returns Promise
 */
export const getSocialInfo = async (id: string) => {
    return axios.get("/user/socialInfo", {
        params: {
            id
        }
    })
}
/**
 * 获取歌手页所需的歌手信息
 * @param userId 用户ID
 * @returns Promise
 */
export const getSingerPageInfo = (userId: string) => {
    return axios({
        url: '/singerInfo/getInfo',
        params: {
            userId
        }
    })
}
/**
 * Todo：当前为获取全部关注的人，需要添加一个API用于获取关注的歌手
 * @param userId 待获取关注者的id
 * @param currentPage 当前页
 * @returns Promise
 */
export const getFollowInfo = (userId: string, currentPage: number) => {
    return axios({
        url: '/follow/follows',
        params: {
            userId,
            currentPage
        }
    })
}

/**
 * 获取粉丝信息 page-size：20
 * @param userId 待获取粉丝信息用户的id
 * @param currentPage 当前页
 * @returns Promise
 */
export const getFansInfo = (userId: string, currentPage: number) => {
    return axios({
        url: '/follow/fans',
        params: {
            userId,
            currentPage
        }
    })
}
/**
 * 参数用session获取
 * @returns 图片信息
 */
export const getNewAvatar = () => {
    return axios({
        url: '/user/avatar'
    })
}
/**
 * 检查是否已经关注某个用户
 * @param userId 待检查用户ID
 * @returns Promise
 */
export const checkFollow = async (userId: string) => {
    return axios({
        url: '/follow/checkFollow',
        params: {
            followedId: userId
        }
    })
}
/**
 * 关注
 * @param userId 待关注用户id
 * @returns Promise
 */
export const follow = (userId: string) => {
    return axios({
        url: '/follow/followUser',
        method: 'post',
        params: {
            followedId: userId
        }
    })
}

/**
 * 收藏歌单
 * @param musicListId 待收藏歌单id
 * @returns Promise
 */
export const collectMusicList = (musicListId: string) => {
    return axios({
        url: '/musicList/collectList',
        method: 'post',
        params: {
            musicListId
        }
    })
}
/**
 * 取关
 * @param userId 待取关用户id
 * @returns Promise
 */
export const unFollow = (userId: string) => {
    return axios({
        url: '/follow/unFollow',
        method: 'post',
        params: {
            followedId: userId
        }
    })
}
/**
 * 获取收藏的歌单
 * @param userId 用户id
 * @returns 
 */
export const getCollectMusicList = (userId: string) => {
    return axios({
        url: '/musicList/getCollectList',
        method: 'get',
        params: {
            userId
        }
    })
}

export const likePost = (id: string) => {
    return axios({
        url: '/post/like',
        method: 'post',
        params: {
            postId: id
        }
    })
}
export const likeComment = (id: string) => {
    return axios({
        url: '/comment/like',
        method: 'post',
        params: {
            postId: id
        }
    })
}
/**
 * 创建新歌单
 * @param listTitle 歌单title
 * @returns Promise
 */
export const newMusicList = (listTitle: string) => {
    return axios({
        url: '/musicList/createList',
        method: 'post',
        params: {
            listTitle
        }
    })
}
/**
 * 根据点赞获取前十条评论
 * @param typeId 类型id
 * @param type 类型 pc/p/m/l
 * @returns Promise
 */
export const getCommentsByLikes = (typeId: string, type: string) => {
    return axios({
        url: '/comment/getComment',
        method: 'get',
        params: {
            currentPage: 1,
            pageSize: 10,
            type,
            typeId,
            sortByLikes: true
        },
    })
}
/**
 * 获取了收藏当前歌单的至多8位听众
 * @param musicListId 歌单id
 * @returns Promise
 */
export const getCollectUsers = (musicListId: string) => {
    return axios({
        url: '/user/getCollectUsers',
        params: {
            musicListId
        }
    })
}
/**
 * 统计播放量
 * @param musicId 歌曲id
 * @param typeId 类型id
 * @param type 类型 single ｜ musiclist ｜ cd 此处single 与 search功能一致，暂不做区分
 * @returns 
 */
export const addPlayTimes = (musicId: string, typeId?: string, type?: "musicList" | "cd" | "singer" | "single" | "search") => {
    return axios({
        url: '/music/addPlayTimes',
        method: 'post',
        params: {
            musicId,
            typeId,
            type
        }
    })
}
/**
 * 获取用户一周的播放次数信息
 * @param musicId 
 * @returns 
 */
export const getWeekPlayTimes = (userID?: string) => {
    return axios({
        url: '/music/getWeekPlay',
        method: 'get',
        params: {
            id: userID
        }
    })
}
/**
 * 以分页形式获取所有的歌单
 * @param currentPage 当前页
 * @returns 
 */
export const getMusicList = (currentPage: number) => {
    return axios({
        url: '/musicList/getMusicList',
        method: 'get',
        params: {
            currentPage
        }
    })
}

/**
 * 搜索单曲
 * @param keyword 
 * @param currentPage 
 * @param pageSize 
 * @returns PageInfo
 */
export const searchMusic = async (keyword: string, currentPage: number, pageSize: number) => {
    return axios({
        url: '/search/music',
        method: 'get',
        params: {
            keyword: encodeURIComponent(keyword),
            currentPage,
            pageSize
        }
    })
}

/**
 * 搜索歌手
 * @param keyword 
 * @param currentPage 
 * @param pageSize 
 * @returns PageInfo
 */
export const searchSinger = (keyword: string, currentPage: number, pageSize: number) => {
    return axios({
        url: '/search/singer',
        method: 'get',
        params: {
            keyword: encodeURIComponent(keyword),
            currentPage,
            pageSize
        }
    })
}

/**
 * 搜索专辑
 * @param keyword 
 * @param currentPage 
 * @param pageSize 
 * @returns PageInfo
 */
export const searchAlbum = (keyword: string, currentPage: number, pageSize: number) => {
    return axios({
        url: '/search/CD',
        method: 'get',
        params: {
            keyword: encodeURIComponent(keyword),
            currentPage,
            pageSize
        }
    })
}


/**
 * 搜索歌单
 * @param keyword 
 * @param currentPage 
 * @param pageSize 
 * @returns PageInfo
 */
export const searchMusicList = (keyword: string, currentPage: number, pageSize: number) => {
    return axios({
        url: '/search/musicList',
        method: 'get',
        params: {
            keyword: encodeURIComponent(keyword),
            currentPage,
            pageSize
        }
    })
}

/**
 * 搜索用户
 * @param keyword 
 * @param currentPage 
 * @param pageSize 
 * @returns PageInfo
 */
export const searchUser = (keyword: string, currentPage: number, pageSize: number) => {
    return axios({
        url: '/search/user',
        method: 'get',
        params: {
            keyword: encodeURIComponent(keyword),
            currentPage,
            pageSize
        }
    })
}

/**
 * 获取首页CD
 * @returns Promise
 */
export const getIndexCD = () => {
    return axios({
        url: '/musicList/getCD',
        method: 'get'
    })
}
export const uploadMusicAPI = (duration: number, title: string, music: string, image: string, intro: string, style: string) => {
    return axios({
        url: '/music/uploadNewMusic',
        method: 'post',
        params: {
            duration,
            title,
            music,
            image,
            intro,
            style
        }
    })
}
export const uploadCDApi = (title: string, image: string, intro: string) => {
    return axios({
        url: '/musicList/uploadNewCD',
        method: 'post',
        params: {
            title,
            image,
            intro
        }
    })
}
export const getUserMusic = () => {
    return axios({
        url: '/music/getUserMusic',
        method: 'get'
    })
}
export const getMusicEditInfo = (musicId: string) => {
    return axios({
        url: '/music/editInfo',
        method: 'get',
        params: {
            musicId
        }
    })
}
export const updateMusic = (musicId: string, duration: number, title: string, music: string, image: string, intro: string, style: string) => {
    return axios({
        url: '/music/updateMusic',
        method: 'post',
        params: {
            duration,
            title,
            music,
            image,
            intro,
            musicId,
            style
        }
    })
}
export const getMusicCD = (id?: string) => {
    return axios({
        url: '/musicList/getUserCD',
        method: 'get',
        params: {
            id
        }
    })
}
export const getCDEditInfo = (cdId: string) => {
    return axios({
        url: '/musicList/getCDEditInfo',
        method: 'get',
        params: {
            id: cdId
        }
    })
}
export const updateCD = (cdId: string, title: string, image: string, introduce: string) => {
    return axios({
        url: '/musicList/updateCD',
        method: 'post',
        params: {
            title,
            image,
            introduce,
            id: cdId
        }
    })
}
export const getCDList = (id: string) => {
    return axios({
        url: '/music/getList',
        method: 'get',
        params: {
            musicListId: id
        }
    })
}

export const getValidUserMusic = () => {
    return axios({
        url: '/music/getMusicWithoutCD',
        method: 'get',
    })
}

export const insertMusicToCD = (data: string) => {
    return axios({
        url: '/musicList/insertMusicToCD',
        method: 'post',
        data: data,
        headers: {
            'Content-Type': 'application/json'
        }
    })
}
export const deleteMusicFromCD = (listId: string, musicId: string) => {
    return axios({
        url: '/musicList/deleteMusicFromCD',
        method: 'post',
        params: {
            listId,
            musicId
        }
    })
}
/**
 * 在评论下发布评论
 * @param content 正文内容
 * @param type 类型
 * @param typeId 类型id
 * @param commentedId 被评论的id
 * @returns 
 */
export const comment = (content: string, type: string, typeId: string, commentedId: string) => {
    return axios.post('/comment/sendComment', {
        content,
        type,
        typeId,
        commentedId
    })
}

export const getAllMusic = (currentPage: number) => {
    return axios.get('/music/getAllMusic', {
        params: {
            currentPage
        }
    }
    )
}
// 评论评论 不知道怎么命名了
export const commentComment = (commentedId: string, content: string, type: string, typeId: string) => {
    return axios({
        url: '/comment/comment',
        method: 'post',
        data: {
            content: content,
            commentedId,
            type,
            typeId
        }
    })
}
export const deleteSingle = (musicId: string) => {
    return axios({
        url: '/music/deleteSingle',
        method: 'post',
        params: {
            musicId
        }
    })
}

export const getRecommendMusicList = (id: string) => {
    return axios.get('/musicList/getRecommendMusicList', {
        params: {
            id
        }
    })
}
export const deleteCD = (id: string) => {
    return axios({
        url: '/musicList/deleteCD',
        method: 'post',
        params: {
            id
        }
    })
}
export const getRecommendSinger = () => {
    return axios.get('/singerInfo/randomSinger')
}

export const getAllplayNums = (userId: string) => {
    return axios.get('/music/allPlaysNums', {
        params: {
            userId
        }
    })
}
